<?php
declare(strict_types=1);
/**
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/8/7
 * Time: 10:07:16
 */

namespace Service\CommodityService;

use App\Model\BbcModel\GoodsService;
use Service\Code\Normal;
use Service\BaseService;

/**
 * 商品服务管理
 */
class GoodsServiceAttribute extends BaseService
{

    public function makeWhere($param){
        $where = [
            ['cid','=',$param['cid']],
            ['sid','=',$param['sid']],
            ['df','=',0],
        ];

        if(isset($param['title']) && $param['title'] != ''){
            $where[] = ['title','like',"%{$param['title']}%"];
        }

        if(isset($param['is_default']) && $param['is_default'] != ''){
            $where[] = ['is_default','=',$param['is_default']];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['status','=',$param['status']];
        }

        return $where;
    }
    public function goodsServiceList($param){
        $page = $param['page'] ?? 1;
        $limit = $param['limit'] ?? 10;

        $where = $this->makeWhere($param);
        $data_list = GoodsService::instance()
            ->where($where)
            ->orderByRaw('show_order desc,id desc')
            ->paginate($limit,['id','title','status','is_default','show_order'],'page',$page);

        $list = $data_list->items();
        $total = $data_list->total();

        foreach ($list as &$v){
            if($v['status'] == 1){
                $v['status_text'] = '启用';
            }else{
                $v['status_text'] = '禁用';
            }

            if($v['is_default'] == 1){
                $v['is_default_text'] = '默认';
            }else{
                $v['is_default_text'] = '否';
            }
        }

        return Normal::returnCode(Normal::SUC,'请求成功',['list'=>$list,'total'=>$total]);
    }

    /**
     * 新增商品服务
     * @param $param
     * @return array
     */
    public function addGoodsService($param){
        if(!$param['title']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务名称');
        }

        if(!$param['content']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务说明');
        }

        $max_sort = GoodsService::instance()
            ->where(['cid'=>$param['cid'],'sid'=>$param['sid'],'df'=>0])
            ->max('show_order');

        $res = GoodsService::instance()
            ->insert([
                'sid' => $param['sid'],
                'cid' => $param['cid'],
                'title' => $param['title'],
                'content' => $param['content'],
                'show_order' => $max_sort + 1,
                'cd' => time(),
                'md' => time(),
            ]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'新增失败');
        }

        return Normal::returnCode(Normal::SUC,'新增成功');
    }

    public function goodsServiceDetails($param){
        if(!$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务ID');
        }

        $info = GoodsService::instance()
            ->where(['id'=>$param['id'],'df'=>0,'cid'=>$param['cid']])
            ->select(['id','title','content','status','is_default','show_order'])
            ->first();

        if(!$info){
            return Normal::returnCode(Normal::FAIL,'商品服务不存在');
        }

        return Normal::returnCode(Normal::SUC,'请求成功',$info);
    }

    /**
     * 编辑商品服务
     * @param $param
     * @return array
     */
    public function editGoodsService($param){
        if(!$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务ID');
        }

        if(!$param['title']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务名称');
        }

        if(!$param['content']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务说明');
        }

        $info = GoodsService::instance()
            ->where(['id'=>$param['id'],'df'=>0,'cid'=>$param['cid']])
            ->first();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'商品服务不存在');
        }

        $show_order = $param['show_order'] ?? $info['show_order'];

        $res = GoodsService::instance()
            ->where(['id'=>$param['id']])
            ->update(['title'=>$param['title'],'content'=>$param['content'],'show_order'=>$show_order,'md'=>time()]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'编辑失败');
        }

        return Normal::returnCode(Normal::SUC,'编辑成功');

    }

    /**
     * 变更商品服务状态
     * @param $param
     * @return array
     */
    public function changeServiceStatus($param){
        if(!$param['service_ids']){
            return Normal::returnCode(Normal::FAIL,'缺少服务ID');
        }

        if(!isset($param['status'])){
            return Normal::returnCode(Normal::FAIL,'缺少状态参数');
        }

        if(!in_array($param['status'],[0,1])){
            return Normal::returnCode(Normal::FAIL,'状态参数不正确');
        }

        $st = GoodsService::instance()
            ->whereIn('id',$param['service_ids'])
            ->where(['cid'=>$param['cid'],'df'=>0])
            ->exists();
        if(!$st){
            return Normal::returnCode(Normal::FAIL,'商品服务不存在');
        }

        $res = GoodsService::instance()
            ->whereIn('id',$param['service_ids'])
            ->update(['status'=>$param['status'],'md'=>time()]);

        if(!$res){
            return Normal::returnCode(Normal::FAIL,'操作失败');
        }

        return Normal::returnCode(Normal::SUC,'操作成功');
    }

    public function changeServiceDefault($param){
        if(!$param['service_ids']){
            return Normal::returnCode(Normal::FAIL,'缺少服务ID');
        }

        if(!isset($param['default'])){
            return Normal::returnCode(Normal::FAIL,'缺少状态参数');
        }

        if(!in_array($param['default'],[0,1])){
            return Normal::returnCode(Normal::FAIL,'状态参数不正确');
        }

        $st = GoodsService::instance()
            ->whereIn('id',$param['service_ids'])
            ->where(['cid'=>$param['cid'],'df'=>0])
            ->exists();
        if(!$st){
            return Normal::returnCode(Normal::FAIL,'商品服务不存在');
        }

        $res = GoodsService::instance()
            ->whereIn('id',$param['service_ids'])
            ->update(['is_default'=>$param['default'],'md'=>time()]);
        if(!$res){
            return Normal::returnCode(Normal::FAIL,'操作失败');
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }

    public function delGoodsService($param) : array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少服务ID');
        }

        $info = GoodsService::instance()
            ->where(['id'=>$param['id'],'df'=>0,'cid'=>$param['cid']])
            ->exists();
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'商品服务不存在');
        }

        $res = GoodsService::instance()
            ->where(['id'=>$param['id'],'df'=>0,'cid'=>$param['cid']])
            ->update(['df'=>1,'md'=>time()]);
        if(!$res){
            return Normal::returnCode(Normal::FAIL,'删除失败');
        }

        return Normal::returnCode(Normal::SUC,'删除成功');
    }

    public function moveUpDown($param){
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务ID');
        }
        if(!isset($param['type']) || !$param['type']){
            return Normal::returnCode(Normal::FAIL,'缺少移动类型参数');
        }
        $table = GoodsService::instance();
        $where = [
            ['cid','=',$param['cid']],
            ['sid','=',$param['sid']],
            ['df','=',0],
        ];
        return (new \Service\MoveUpDownService\MoveUpDownService())->moveUpDown($param['id'],$param['type'],$table,$where,'show_order');
    }

    public function setTop($param) : array{
        if(!isset($param['id']) || !$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少商品服务ID');
        }

        $table = GoodsService::instance();
        $where = [
            ['cid','=',$param['cid']],
            ['sid','=',$param['sid']],
            ['df','=',0],
        ];
        $res = (new \Service\MoveUpDownService\MoveUpDownService())->setTop($param['id'],$table,$where,'show_order',$param['type']);
        if($res['code'] != Normal::SUC){
            return Normal::returnCode(Normal::FAIL,$res['msg']);
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }
}